package com.diet.system.service.impl;

import com.diet.system.domain.DietUserBehavior;
import com.diet.system.mapper.DietUserBehaviorMapper;
import com.diet.system.service.IDietUserBehaviorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

@Service
public class DietUserBehaviorServiceImpl implements IDietUserBehaviorService {

    @Autowired
    private DietUserBehaviorMapper dietUserBehaviorMapper;
        @Override
    public List<DietUserBehavior> selectUserRatings(Long userId) {
        DietUserBehavior query = new DietUserBehavior();
        query.setUserId(userId);
        query.setInteractionType("评分");
        return dietUserBehaviorMapper.selectDietUserBehaviorList(query);
    }

@Override
public List<DietUserBehavior> selectUserClicks(Long userId) {
    DietUserBehavior query = new DietUserBehavior();
    query.setUserId(userId);
    query.setInteractionType("点击");
    return dietUserBehaviorMapper.selectDietUserBehaviorList(query);
}

    @Override
    public boolean hasUserRated(Long userId, Long dishId) {
        // 1. 查询用户是否已经对该菜品进行过评分
        DietUserBehavior existingBehavior = getUserRatingBehavior(userId, dishId);
        return existingBehavior != null;
    }
    /**
     * 根据用户ID和菜品ID获取评分行为记录
     */
    private DietUserBehavior getUserRatingBehavior(Long userId, Long dishId) {
        DietUserBehavior query = new DietUserBehavior();
        query.setUserId(userId);
        query.setDishId(dishId);
        query.setInteractionType("评分");

        List<DietUserBehavior> behaviors = dietUserBehaviorMapper.selectDietUserBehaviorList(query);
        return behaviors.isEmpty() ? null : behaviors.get(0);
    }



    @Override
    public List<DietUserBehavior> selectDietUserBehaviorList(DietUserBehavior dietUserBehavior) {
        return dietUserBehaviorMapper.selectDietUserBehaviorList(dietUserBehavior);
    }

    @Override
    public DietUserBehavior selectDietUserBehaviorById(Long id) {
        return dietUserBehaviorMapper.selectDietUserBehaviorById(id);
    }

    @Override
    public int insertDietUserBehavior(DietUserBehavior dietUserBehavior) {
        return dietUserBehaviorMapper.insertDietUserBehavior(dietUserBehavior);
    }

    @Override
    public int updateDietUserBehavior(DietUserBehavior dietUserBehavior) {
        return dietUserBehaviorMapper.updateDietUserBehavior(dietUserBehavior);
    }

    @Override
    public int deleteDietUserBehaviorById(Long id) {
        return dietUserBehaviorMapper.deleteDietUserBehaviorById(id);
    }

    @Override
    public int deleteDietUserBehaviorByIds(Long[] ids) {
        return dietUserBehaviorMapper.deleteDietUserBehaviorByIds(ids);
        }@Override
public boolean clickDish(Long userId, Long dishId) {
    // 1. 查询用户是否已经点击过该菜品
    DietUserBehavior existingBehavior = getUserClickBehavior(userId, dishId);

    if (existingBehavior != null) {
        // 2. 如果已存在点击记录，则更新点击次数
        existingBehavior.setCount(existingBehavior.getCount() + 1);
        return updateDietUserBehavior(existingBehavior) > 0;
    } else {
        // 3. 如果不存在点击记录，则新增一条点击记录
        DietUserBehavior behavior = new DietUserBehavior();
        behavior.setUserId(userId);
        behavior.setDishId(dishId);
        behavior.setInteractionType("点击");
        behavior.setCount(1);
        return insertDietUserBehavior(behavior) > 0;
    }
}

/**
 * 根据用户ID和菜品ID获取点击行为记录
 */
private DietUserBehavior getUserClickBehavior(Long userId, Long dishId) {
    DietUserBehavior query = new DietUserBehavior();
    query.setUserId(userId);
    query.setDishId(dishId);
    query.setInteractionType("点击");

    List<DietUserBehavior> behaviors = dietUserBehaviorMapper.selectDietUserBehaviorList(query);
    return behaviors.isEmpty() ? null : behaviors.get(0);
}


    @Override
    public boolean toggleFavorite(Long userId, Long dishId) {
        DietUserBehavior existing = dietUserBehaviorMapper.selectUserFavoriteByUserAndDish(userId, dishId);
        if (existing != null) {
            // 取消收藏
            dietUserBehaviorMapper.deleteDietUserBehaviorById(existing.getId());
            return false;
        } else {
            // 添加收藏
            DietUserBehavior behavior = new DietUserBehavior();
            behavior.setUserId(userId);
            behavior.setDishId(dishId);
            behavior.setInteractionType("收藏");
            dietUserBehaviorMapper.insertDietUserBehavior(behavior);
            return true;
        }
    }

    @Override
    public List<DietUserBehavior> selectUserFavorites(Long userId) {
        DietUserBehavior query = new DietUserBehavior();
        query.setUserId(userId);
        query.setInteractionType("收藏");
        return dietUserBehaviorMapper.selectDietUserBehaviorList(query);
    }

    @Override
    public boolean rateDish(Long userId, Long dishId, Integer rating) {
        DietUserBehavior behavior = new DietUserBehavior();
        behavior.setUserId(userId);
        behavior.setDishId(dishId);
        behavior.setInteractionType("评分");
        behavior.setRating(rating);
        return dietUserBehaviorMapper.insertDietUserBehavior(behavior) > 0;
    }
    
    @Override
    public Map<String, Object> getUserDishStatus(Long userId, List<Long> dishIds) {
        Map<String, Object> result = new HashMap<>();
        Map<Long, Boolean> favoriteStatus = new HashMap<>();
        Map<Long, Integer> ratingStatus = new HashMap<>();
        
        for (Long dishId : dishIds) {
            // 检查收藏状态
            DietUserBehavior favoriteQuery = new DietUserBehavior();
            favoriteQuery.setUserId(userId);
            favoriteQuery.setDishId(dishId);
            favoriteQuery.setInteractionType("收藏");
            List<DietUserBehavior> favorites = dietUserBehaviorMapper.selectDietUserBehaviorList(favoriteQuery);
            favoriteStatus.put(dishId, !favorites.isEmpty());
            
            // 检查评分状态
            DietUserBehavior ratingQuery = new DietUserBehavior();
            ratingQuery.setUserId(userId);
            ratingQuery.setDishId(dishId);
            ratingQuery.setInteractionType("评分");
            List<DietUserBehavior> ratings = dietUserBehaviorMapper.selectDietUserBehaviorList(ratingQuery);
            if (!ratings.isEmpty()) {
                ratingStatus.put(dishId, ratings.get(0).getRating());
            }
        }
        
        result.put("favorites", favoriteStatus);
        result.put("ratings", ratingStatus);
        return result;
    }
}
